package com.lcf.system.manager;

import com.lcf.framework.vo.TreeVO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：树形结构
 *
 * @author lcf
 * @since 2025/3/10 16:29
 */
@Component
public class TreeManager {
    /**
     * 构建树
     */
    public <T extends TreeVO<T>>List<T> buildMenuTree(List<T> allMenus, Long parentId) {
        List<T> result = new ArrayList<>();

        for (T menu : allMenus) {
            if (parentId.equals(menu.getParentId())) {
                // 找到一级菜单或者符合当前父ID的菜单
                List<T> children = buildMenuTree(allMenus, menu.getId());
                menu.setChildren(children);
                result.add(menu);
            }
        }

        return result;
    }
}
